/*实现级联勾选开始*/
var nodeCheckedSilent = false;
function nodeChecked (event, node){
    if(nodeCheckedSilent){
        return;
    }
    nodeCheckedSilent = true;
    checkAllParent(node);
    checkAllSon(node);
    nodeCheckedSilent = false;
}

var nodeUncheckedSilent = false;
function nodeUnchecked  (event, node){
    if(nodeUncheckedSilent)
        return;
    nodeUncheckedSilent = true;
    uncheckAllParent(node);
    uncheckAllSon(node);
    nodeUncheckedSilent = false;
}

//选中全部父节点
function checkAllParent(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)){
        return;
    }else{
        checkAllParent(parentNode);
    }
}
//取消全部父节点
function uncheckAllParent(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    var siblings = $('#tree').treeview('getSiblings', node.nodeId);
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)) {
        return;
    }
    var isAllUnchecked = true;  //是否全部没选中
    for(var i in siblings){
        if(siblings[i].state.checked){
            isAllUnchecked=false;
            break;
        }
    }
    if(isAllUnchecked){
        uncheckAllParent(parentNode);
    }

}

//级联选中所有子节点
function checkAllSon(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            checkAllSon(node.nodes[i]);
        }
    }
}
//级联取消所有子节点
function uncheckAllSon(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            uncheckAllSon(node.nodes[i]);
        }
    }
}
/*实现级联勾选结束*/


$("#menu_level01").dialog({});
$("#menu_level02").dialog({});
//二级table显示
function detail(index,row){
    var param="id="+row.deId;
    var html=[];
    $.ajax({
        url:ctx+"/sys/function/powers",
        data:param,
        async:false,
		type:'post',
        dataType:'json',
        success:function(arr){
        	html.push('<table class="table-hover table table-condensed" style="border: 1px solid rgb(221, 221, 221);"><tr class="active">\
					 <td>角色编号</td>\
					 <td>职称名称</td>\
					 <td>操作</td></tr>');
			 $.each(arr,function(){
				 html.push('<tr id="list">\
				 <td class="info">'+this.roleId+'</td>\
				 <td class="success">'+this.roleName+'</td>\
				 <td class="active" >\
				    <a class="edit2" href="javascript:void(0)" onclick="update2('+this.roleId+')" title="修改">\
				 	<i class="glyphicon glyphicon-edit"></i></a>&nbsp;&nbsp;\
				 	<a class="del2" href="javascript:void(0)" onclick="del2('+this.roleId+')" title="删除">\
				 	<i class="glyphicon glyphicon-remove"></i></a></td></tr>');
			 })
			 html.push('</table>');
        }
    });
    return html.join("");
}

/**
 * 添加一级菜单
 */
$(".add1").click(function(){
    var $form = $("#menu_level01 form").clone();
    $("#menu_level01").children().remove();
    $form.appendTo("#menu_level01");
	$("#menuEdit01 input[name=funcId]").val("");
	$("#menuEdit01 input[name=functionCode]").val("");
	$("#menuEdit01 input[name=functionName]").val("");
	$("#menuEdit01 input[name=funcUrl]").val("");
	$('#menu_level01').dialog({
		title:"添加一级菜单",
		btnName:"确认",
		addBind: function(){
			var params = $("#menuEdit01").serialize();
			$.post(ctx+"/sys/function/inserts",params,function(msg){
				$("#menu_level01").dialog("close");
	    		toastr.info(msg)
 				$("#functionTable").bootstrapTable("refresh");
			});
		}
	});
	$("#menu_level01").dialog("open");
});
/**
 * 添加二级菜单
 */
$(".add2").click(function(){
    var $form = $("#menu_level02 form").clone();
    $("#menu_level02").children().remove();
    $form.appendTo("#menu_level02");
	$("#menuEdit02 input[name=funcId]").val("");
	$("#menuEdit02 input[name=functionCode]").val("");
	$("#menuEdit02 input[name=functionName]").val("");
	$('#menu_level02').dialog({
		title:"添加二级菜单",
		btnName:"确认",
		addBind: function(){
			var params = $("#menuEdit02").serialize();
			console.log(params);
			$.post(ctx+"/sys/function/playadd",params,function(msg){
				$("#menu_level02").dialog("close");
	    		toastr.info(msg);
 				$("#menuTable").bootstrapTable("refresh");
			});
		}
	});
    /**
     * 获取父级编号
     */
    $.post(ctx+"/sys/function/list",function(lists){
        $.each(lists,function(){
            var opts=$("<option value="+this.deId+">"+this.deName+"</option>");
            $("#parentId").append(opts);
        });
    },'json');
	$("#menu_level02").dialog("open");
});



/**
 * 操作按钮
 */
function fmtOpt(){
	var edit="<a class='edit' href='javascript:void(0)' title='修改'><i class='glyphicon glyphicon-edit'></i></a>";
	var del="<a class='del' href='javascript:void(0)' title='删除'><i class='glyphicon glyphicon-remove'></i></a>";
	return edit+"&nbsp;&nbsp;"+del;
}
window.optEvents={
	"click .edit":function(e,value,row,index){
		update(row);
	},
	"click .del":function(e,value,row,index){
		del(row);
	}
}

/**
 * 编辑权限
 */
function Initupdate(){
    var $form = $("#menu_level01 form").clone();
    $("#menu_level01").children().remove();
    $form.appendTo("#menu_level01");
	$("#menu_level01").dialog({
		title:"编辑权限菜单",
		btnName:"确认",
		addBind: function(){
			var params = $("#menuEdit01").serialize();
			$.post(ctx+"/sys/function/editMenu",params,function(msg){
				$("#menu_level01").dialog("close");
	    		toastr.info(msg);
 				$("#functionTable").bootstrapTable("refresh");
			});
		}
	});
}

function update(row) {
    $("#menu_level01 form").setForm(row);
    $("#menu_level01").dialog("open");
}

/**
 * 删除权限
 */
function del(row){
	$.post("sys/function/getFunc/"+row.funcId,function(msg){
		 toastr.warning(msg);
		 $("#functionTable").bootstrapTable("refresh");
	},'json')
}



/*
*
*角色授权
* */





$(function(){
    Initupdate();
    initGrant();
});

function initGrant(){
    $("#grant").dialog({
        title:'管理授权',
        btnValue:"授权",
        addBind:function(){
            var rows=$("#functionTable").bootstrapTable("getSelections");

            var param="deId="+rows[0].deId;
            var arr=$("#tree").treeview("getChecked");
            $(arr).each(function(){
                param=param+"&ids="+this.id;
            });
            $.post(ctx+"/sys/permission/grant",param,function(msg){
                toastr.warning("信息提示",msg);
                $("#grant").dialog("close");
                $("#functionTable").bootstrapTable("uncheckAll");
            },'text');
        }
    });
}

function grant() {
    var rows=$("#functionTable").bootstrapTable("getSelections");
    if(rows.length!=1){
        swal("请选择一个管理待授权！");
        return;
    }
    $.getJSON(ctx+"/sys/permission/getTree","deId="+rows[0].deId,function(result){
        $("#tree").treeview({
            data:result,
            showCheckbox:true,
            multiSelect:true,
            onNodeChecked:nodeChecked,
            onNodeUnchecked:nodeUnchecked
        });
        $("#grant").dialog("open");
    });

}


/**
 * 修改二级权限
 */
function update2(roleId){
    $("#menu_level02 [name=roleId]").val(roleId)
    var $form = $("#menu_level02 form").clone();
    $("#menu_level02").children().remove();
    $form.appendTo("#menu_level02");
	$.post(ctx+"/sys/function/list",null,function(list){
	    $(list).each(function () {
            var $opt = $("<option/>")
            $opt.val(this.deId);
            $opt.html(this.deName)
            $opt.appendTo("#parentId")
        })
        $("#parentId option").each(function(){
            if($(this).val()==parentId){
                $(this).attr("selected","selected");
            }
        })
		/*$("#menuEdit02 input[name=funcId]").val(list.funcId);
		$("#menuEdit02 input[name=functionNames]").val(list.roleName);
		var parentId = list.parentId;
		$("#parentId option").each(function(){
			if($(this).val()==parentId){
				$(this).attr("selected","selected");
			}
		})*/
	},'json');
	$("#menu_level02").dialog({
		title:"编辑权限菜单",
		btnName:"确认",
		addBind: function(){
			var params = $("#menuEdit02").serialize();
			console.log(params)
			$.post(ctx+"/sys/function/roleUpdate",params,function(msg){
				$("#menu_level02").dialog("close");
	    		toastr.info(msg);
 				$("#functionTable").bootstrapTable("refresh");
			});
		}
	});
	$("#menu_level02").dialog("open");
}
/*
 * 删除二级权限
 */
function del2(roleId){

	$.post(ctx+"/sys/function/getFunc","roleId="+roleId,function(msg){
	    if(msg>0){
            toastr.info("删除成功");
        }else{
            toastr.info("删除失败");
        }
		 $("#functionTable").bootstrapTable("refresh");
	},'text')
}